package com.mikey.emoji.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.mikey.emoji.common.ApiRestResponse;
import com.mikey.emoji.exception.YunzhiMallExceptionEnum;
import com.mikey.emoji.model.pojo.Paper;
import com.mikey.emoji.model.request.AddEmojiReq;
import com.mikey.emoji.model.request.DeletePaperReq;
import com.mikey.emoji.model.request.UpdateReviewStatusReq;
import com.mikey.emoji.service.PaperService;
import com.mikey.emoji.tencent.TencentCosManager;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * @author :Mikey
 * @description :
 * @create :2022-04-12 23:30:00
 */
@RestController
@RequestMapping("/emoji")
//配置跨域
//@CrossOrigin(origins = {"http://10.55.135.40:8087"})
@Slf4j
public class PaperController {
    @Autowired
   private PaperService paperService;
    @Resource
    private TencentCosManager tencentCosManager;
    @GetMapping("/selectKey")
    public ApiRestResponse selectKey(@RequestParam("key")String key){
       if (StringUtils.isBlank(key)){
           List<Paper> list = paperService.list();
           return ApiRestResponse.success(list);
       }
        QueryWrapper<Paper> paperQueryWrapper = new QueryWrapper<>();
        paperQueryWrapper.eq("name",key);
        List<Paper> list = paperService.list(paperQueryWrapper);
        if (list.size()>0){
            return ApiRestResponse.success(list);
        }
        return ApiRestResponse.error(YunzhiMallExceptionEnum.SELECT_ERROR);
    }

    /**
     * 文件上传
     * @param file
     * @return
     * @throws IOException
     */
    @PostMapping("/upload")
    public ApiRestResponse uploadFile(MultipartFile file) throws IOException {
        File localFile = File.createTempFile("temp", null);
        String fileName = System.currentTimeMillis() / 1000 + "-" + file.getOriginalFilename();
        try {
            file.transferTo(localFile);
        } catch (IOException e) {
            e.printStackTrace();
            return ApiRestResponse.error(YunzhiMallExceptionEnum.FILE_UPLOAD_ERROR);
        }
        String resultPath = tencentCosManager.uploadFile(fileName, localFile);
        // 清理临时文件
        boolean deleteResult = localFile.delete();
        if (!deleteResult) {
            log.error("fileDelete error", localFile.getName());
        }
        return ApiRestResponse.success(resultPath);
    }

    /**
     * 增加表情
     * @param addEmojiReq
     * @return
     */
    @PostMapping("/add")
 public ApiRestResponse addEmoji(@RequestBody @Valid AddEmojiReq addEmojiReq){
     Paper paper = new Paper();
     BeanUtils.copyProperties(addEmojiReq,paper);
     boolean flag = paperService.save(paper);
     if (flag){
         return ApiRestResponse.success();
     }
     return ApiRestResponse.error(YunzhiMallExceptionEnum.ADD_ERROR);
 }

    /**
     * 查询全部表情
     *
     * @return
     */
 @GetMapping("/findAll")
 public ApiRestResponse findAll(@RequestParam("reviewStatus") Integer reviewStatus){
     if (reviewStatus!=null){
         QueryWrapper<Paper> paperQueryWrapper = new QueryWrapper<>();
         paperQueryWrapper.eq("reviewStatus",reviewStatus);
         List<Paper> papers = paperService.list(paperQueryWrapper);
         if (papers.size()>0){
             return ApiRestResponse.success(papers);
         }
         return ApiRestResponse.error(YunzhiMallExceptionEnum.SELECT_ERROR);
     }
     return ApiRestResponse.error(YunzhiMallExceptionEnum.REQUEST_PARAM_ERROR);
 }

    /**
     * 更新审核状态
     * @param updateReviewStatusReq
     * @return
     */
 @PostMapping("/update")
 public ApiRestResponse reviewStatus(@RequestBody @Valid UpdateReviewStatusReq updateReviewStatusReq){
         UpdateWrapper<Paper> paperUpdateWrapper = new UpdateWrapper<>();
         paperUpdateWrapper.eq("id",updateReviewStatusReq.getId()).set("reviewStatus",updateReviewStatusReq.getReviewStatus());
         boolean update = paperService.update(paperUpdateWrapper);
         if (update){
             return ApiRestResponse.success();
         }
         return  ApiRestResponse.error(YunzhiMallExceptionEnum.UPDATE_ERROR);
 }

    /**
     * 删除表情
     * @param deletePaperReq
     * @return
     */
 @PostMapping("/delete")
 public ApiRestResponse deletePaper(@RequestBody DeletePaperReq deletePaperReq){
     QueryWrapper<Paper> paperQueryWrapper = new QueryWrapper<>();
     paperQueryWrapper.eq("id",deletePaperReq.getId());
     boolean remove = paperService.remove(paperQueryWrapper);
     if (remove){
         return ApiRestResponse.success();
     }else {
         return  ApiRestResponse.error(YunzhiMallExceptionEnum.DELETE_ERROR);
     }
 }
}
